Add functions waypt_speed() and waypt_time().
authoroliskoli <oliskoli>
Mon, 26 Mar 2007 22:18:53 +0000 (22:18 +0000)
committeroliskoli <oliskoli>
Mon, 26 Mar 2007 22:18:53 +0000 (22:18 +0000)
waypt_speed calculates the speed between two points (in meters per second).
waypt_time returns the creation_time of a waypoint including microseconds in fractional portion.

defs.h
waypt.c

diff --git a/defs.h b/defs.h
index 28609b2b45b2bf66cc5e981fe9c53324a125d112..0eb901d9ebe6e5c6fb4d013a5c6384ff081cf201 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -662,6 +662,8 @@ void waypt_init(void);
 void route_init(void);
 void waypt_disp(const waypoint *);
 void waypt_status_disp(int total_ct, int myct);
+double waypt_time(const waypoint *wpt);
+double waypt_speed(const waypoint *A, const waypoint *B);
 
 NORETURN fatal(const char *, ...) PRINTFLIKE(1, 2);
 void is_fatal(const int condition, const char *, ...) PRINTFLIKE(2, 3);
diff --git a/waypt.c b/waypt.c
index 41ce3d3d35eaf8076aa3ea20f01b7ae0bc856161..bc076ff26d3255601cdd638c45ddd88fd1dcbf80 100644 (file)
--- a/waypt.c
+++ b/waypt.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include "defs.h"
 #include "cet_util.h"
+#include "grtcirc.h"
 
 queue waypt_head;
 static unsigned int waypt_ct;
@@ -444,3 +445,39 @@ waypt_add_url(waypoint *wpt, char *link, char *url_link_text)
                }
        }
 }
+
+/*
+ * returns full creation_time with parts of seconds in fractional portion
+ */
+double
+waypt_time(const waypoint *wpt)
+{
+       if (wpt->creation_time <= 0)
+               return (double) 0;
+       else
+               return ((double)wpt->creation_time + ((double)wpt->microseconds / 1000000));
+}
+
+/*
+ * calculates the speed between points "A" and "B"
+ * the result comes in meters per second and is always positive
+ */ 
+
+double
+waypt_speed(const waypoint *A, const waypoint *B)
+{
+       double dist, time;
+       
+       dist = radtometers(gcdist(
+               RAD(A->latitude), RAD(A->longitude),
+               RAD(B->latitude), RAD(B->longitude)));
+       if (dist < 0.1) dist = 0;       /* calc. diffs on 32- and 64-bit hosts */
+       if (dist == 0) return 0;
+       
+       time = fabs(waypt_time(A) - waypt_time(B));
+       if (time > 0)
+               return (dist / time);
+       else
+               return 0;
+}